<template>
    <el-dialog :close-on-click-modal="false"
               :title="!dataForm.prodCommId ? '新增' : '修改'"
               :visible.sync="visible">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 @keyup.enter.native="dataFormSubmit()"
                 label-width="80px"
                 ref="dataForm">

            <div v-if="!isEdit">
                <el-form-item label="评论内容"
                              prop="userName">
                    <el-input :readonly='true'
                              type="textarea"
                              v-model="dataForm.content"></el-input>
                </el-form-item>

                <el-form-item label="评论图片"
                              prop="userName">
                    <div v-if="dataForm.pics==null || dataForm.pics.length>0">
                        无
                    </div>
                    <img :key="item"
                         :src="dialogImageUrl + item"
                         max-width="100%"
                         v-else
                         v-for="item in dataForm.pics">
                </el-form-item>

                <el-form-item label="记录时间"
                              prop="userName">
                    <el-input :readonly='true'
                              v-model="dataForm.recTime"></el-input>
                </el-form-item>
                <el-form-item :readonly='true'
                              label="回复时间"
                              prop="userName">
                    <el-input :readonly='true'
                              v-model="dataForm.replyTime"></el-input>
                </el-form-item>
                <el-form-item label="IP来源"
                              prop="userName">
                    <el-input :readonly='true'
                              v-model="dataForm.postip"></el-input>
                </el-form-item>
                <el-form-item label="得分"
                              prop="score">
                    <el-input :readonly='true'
                              v-model="dataForm.score"></el-input>
                </el-form-item>

                <el-form-item label="是否匿名"
                              prop="isAnonymous"
                              size="mini">
                    <el-radio-group :disabled='true'
                                    v-model="dataForm.isAnonymous">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="0">不是</el-radio>
                    </el-radio-group>
                </el-form-item>
            </div>

            <el-form-item label="掌柜回复"
                          prop="userName"
                          type="textarea">
                <el-input :readonly='!isEdit'
                          v-model="dataForm.replyContent"></el-input>
            </el-form-item>

            <el-form-item label="审核"
                          prop="status"
                          size="mini"
                          v-if="isEdit">
                <el-radio-group :readonly='true'
                                v-model="dataForm.status">
                    <el-radio :label="1">审核通过</el-radio>
                    <el-radio :label="-1">不通过</el-radio>
                    <el-radio :label="0">等待审核</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <span class="dialog-footer"
              slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="dataFormSubmit()"
                 type="primary"
                 v-if="isEdit">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data() {
            return {
                isEdit: false,
                visible: false,
                roleList: [],
                dataForm: {
                    prodCommId: null,
                    prodId: null,
                    orderItemId: null,
                    userId: null,
                    content: null,
                    replyContent: null,
                    recTime: null,
                    replyTime: null,
                    replySts: null,
                    postip: null,
                    score: null,
                    usefulCounts: null,
                    photoJson: null,
                    isAnonymous: null,
                    status: null
                },
                dataRule: {},
                resourcesUrl: process.env.VUE_APP_RESOURCES_URL
            }
        },
        methods: {
            init(prodCommId, isEdit) {
                this.isEdit = isEdit
                this.dataForm.prodCommId = prodCommId || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.prodCommId) {
                        this.$http({
                            url: this.$http.adornUrl('/prod/prodComm/info/' + this.dataForm.prodCommId),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            this.dataForm = data
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl('/prod/prodComm'),
                            method: this.dataForm.prodCommId ? 'put' : 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        })
                    }
                })
            }
        }
    }
</script>
